# FFmpeg 安装脚本 (PowerShell)
# 此脚本将尝试通过多种方法安装 FFmpeg

Write-Host "=== FFmpeg 安装脚本 ===" -ForegroundColor Green
Write-Host "正在检查 FFmpeg 是否已安装..." -ForegroundColor Yellow

# 检查 FFmpeg 是否已安装
try {
    $ffmpegVersion = ffmpeg -version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ FFmpeg 已安装!" -ForegroundColor Green
        Write-Host "版本信息:" -ForegroundColor Cyan
        ffmpeg -version | Select-Object -First 1
        exit 0
    }
} catch {
    Write-Host "FFmpeg 未安装，开始安装..." -ForegroundColor Yellow
}

# 方法1: 尝试使用 Chocolatey
Write-Host "`n方法1: 尝试使用 Chocolatey 安装..." -ForegroundColor Cyan
try {
    $chocoVersion = choco --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Chocolatey 已安装，正在安装 FFmpeg..." -ForegroundColor Green
        choco install ffmpeg -y
        if ($LASTEXITCODE -eq 0) {
            Write-Host "✓ FFmpeg 通过 Chocolatey 安装成功!" -ForegroundColor Green
            # 刷新环境变量
            $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
            exit 0
        }
    } else {
        Write-Host "Chocolatey 未安装，跳过此方法" -ForegroundColor Yellow
    }
} catch {
    Write-Host "Chocolatey 方法失败，尝试其他方法..." -ForegroundColor Yellow
}

# 方法2: 尝试使用 winget
Write-Host "`n方法2: 尝试使用 winget 安装..." -ForegroundColor Cyan
try {
    $wingetVersion = winget --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ winget 已安装，正在安装 FFmpeg..." -ForegroundColor Green
        winget install "Gyan.FFmpeg"
        if ($LASTEXITCODE -eq 0) {
            Write-Host "✓ FFmpeg 通过 winget 安装成功!" -ForegroundColor Green
            # 刷新环境变量
            $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
            exit 0
        }
    } else {
        Write-Host "winget 未安装，跳过此方法" -ForegroundColor Yellow
    }
} catch {
    Write-Host "winget 方法失败，尝试手动下载..." -ForegroundColor Yellow
}

# 方法3: 手动下载和安装
Write-Host "`n方法3: 手动下载 FFmpeg..." -ForegroundColor Cyan
try {
    $downloadUrl = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
    $downloadPath = "$env:TEMP\ffmpeg.zip"
    $extractPath = "C:\ffmpeg"
    
    Write-Host "正在下载 FFmpeg..." -ForegroundColor Yellow
    Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadPath -UseBasicParsing
    
    Write-Host "正在解压 FFmpeg..." -ForegroundColor Yellow
    if (Test-Path $extractPath) {
        Remove-Item $extractPath -Recurse -Force
    }
    Expand-Archive -Path $downloadPath -DestinationPath "C:\" -Force
    
    # 查找解压后的目录
    $ffmpegDir = Get-ChildItem "C:\" -Directory | Where-Object { $_.Name -like "ffmpeg-*" } | Select-Object -First 1
    if ($ffmpegDir) {
        Rename-Item $ffmpegDir.FullName $extractPath
        $binPath = "$extractPath\bin"
        
        # 添加到系统 PATH
        Write-Host "正在添加到系统 PATH..." -ForegroundColor Yellow
        $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
        if ($currentPath -notlike "*$binPath*") {
            [Environment]::SetEnvironmentVariable("Path", "$currentPath;$binPath", "Machine")
            Write-Host "✓ FFmpeg 已添加到系统 PATH" -ForegroundColor Green
        }
        
        # 刷新当前会话的环境变量
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
        
        # 清理下载文件
        Remove-Item $downloadPath -Force
        
        Write-Host "✓ FFmpeg 手动安装成功!" -ForegroundColor Green
        Write-Host "安装路径: $extractPath" -ForegroundColor Cyan
        
        # 验证安装
        try {
            ffmpeg -version | Select-Object -First 1
            Write-Host "✓ FFmpeg 安装验证成功!" -ForegroundColor Green
        } catch {
            Write-Host "⚠ 请重启终端或重新登录以使 PATH 变更生效" -ForegroundColor Yellow
        }
        
        exit 0
    }
} catch {
    Write-Host "手动下载失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 如果所有方法都失败
Write-Host "`n❌ 所有自动安装方法都失败了" -ForegroundColor Red
Write-Host "请手动安装 FFmpeg:" -ForegroundColor Yellow
Write-Host "1. 访问 https://ffmpeg.org/download.html" -ForegroundColor White
Write-Host "2. 下载 Windows 版本" -ForegroundColor White
Write-Host "3. 解压到 C:\ffmpeg" -ForegroundColor White
Write-Host "4. 将 C:\ffmpeg\bin 添加到系统 PATH" -ForegroundColor White
Write-Host "5. 重启终端" -ForegroundColor White

exit 1